package com.beep.erp.common.glide;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.beep.erp.R;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
 * FileName: GlideImageLoader
 * description: 图片加载类,统一适配(方便换库,方便管理)
 */
public class GlideImageLoader {

    public static void load(Context context, @DrawableRes int imageRes, ImageView view) {
        Glide.with(context).load(imageRes).crossFade().into(view);
    }

    public static void load(Context context, String imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .crossFade()
                // .centerCrop()
                .placeholder(R.mipmap.event)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void load1(Context context, String imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .crossFade()
                .centerCrop()
                .placeholder(R.mipmap.banner)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void load2(Context context, String imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .crossFade()
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void load3(Context context, String imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void load3(Context context, int imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void load4(Context context, String imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .centerCrop()
                .crossFade()
                .placeholder(R.mipmap.event)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    /**
     * 比例显示图片
     * @param context
     * @param imageUrl
     * @param imageView
     */

    public static void loadIntoUseFitWidth(Context context, final String imageUrl, final ImageView imageView) {
        Glide.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        //float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = (int) ((float) vw / (float) 1.92);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .placeholder(R.mipmap.event)
                .into(imageView);
    }

    /**
     * 根据屏幕宽度 加载图片
     * @param context
     * @param imageUrl
     * @param imageView
     */

    public static void loadIntoUseFitWidth1(Context context, final String imageUrl, final ImageView imageView) {
        Glide.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .placeholder(R.mipmap.event)
                .into(imageView);
    }

    public static void loadIntoUseFitWidthRadius(Context context, final String imageUrl, final ImageView imageView, int radius) {
        Glide.with(context)
                .load(imageUrl)
//                .transform(new GlideRoundTransform(context, radius))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .placeholder(R.mipmap.event)
                .into(imageView);
    }


    /**
     * 下载图片转换圆角
     *
     * @param context
     * @param imageRes
     * @param view
     * @param radius   圆角度
     */
    public static void loadAdapterRadius(Context context, String imageRes, ImageView view, int radius) {
        DrawableRequestBuilder requestBuilder = Glide.with(context)
                .load(imageRes)
                .centerCrop()
                .crossFade()
                .placeholder(R.mipmap.event)
                .transform(new GlideRoundTransform(context, radius))
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        requestBuilder.into(view);
    }

    /**
     * 下载图片转换圆角
     *
     * @param context
     * @param imageRes
     * @param view
     * @param radius
     */
    public static void loadAdapterRadius(Context context, @DrawableRes int imageRes, ImageView view, int radius) {
        DrawableRequestBuilder requestBuilder = Glide.with(context)
                .load(imageRes)
                .centerCrop()
                .crossFade()
                .transform(new GlideRoundTransform(context, radius))
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        requestBuilder.into(view);
    }

    /**
     * 下载头像转换圆形
     *
     * @param context
     * @param imageRes
     * @param view
     */
    public static void loadAdapterCircle(Context context, String imageRes, ImageView view) {
        RequestManager glideRequest = Glide.with(context);
        glideRequest.load(imageRes)
                .centerCrop()
                .crossFade()//动画
                .placeholder(R.mipmap.user_icon)//占位图片
                .transform(new GlideCircleTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);

    }

    public static void loadAdapterCircle(Context context, int imageRes, ImageView view) {
        RequestManager glideRequest = Glide.with(context);
        glideRequest.load(imageRes)
                .centerCrop()
                .crossFade()//动画
                .placeholder(R.mipmap.user_icon)//占位图片
                .transform(new GlideCircleTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);

    }

    public static void loadAdapterBlur(Context context, String imageRes, ImageView view) {
        RequestManager glideRequest = Glide.with(context);
        glideRequest.load(imageRes)
                .centerCrop()
                .crossFade()//动画
                .placeholder(R.mipmap.mine_bg)//占位图片
                .transform(new GlideBlurTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);

    }

    public static void loadAdapterBlur(Context context, int imageRes, ImageView view) {
        RequestManager glideRequest = Glide.with(context);
        glideRequest.load(imageRes)
                .centerCrop()
                .crossFade()//动画
                .placeholder(R.mipmap.mine_bg)//占位图片
                .transform(new GlideBlurTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);

    }

    public static void clear(Context context) {
        Glide.get(context).clearMemory();
    }
}
